package com.yegs.admin.business.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.yegs.admin.business.entity.BaseMenu;
import com.yegs.admin.business.entity.BaseRole;
import com.yegs.admin.business.entity.BaseRoleMenu;
import com.yegs.admin.business.mapper.BaseMenuMapper;
import com.yegs.admin.business.mapper.BaseRoleMapper;
import com.yegs.admin.business.mapper.BaseRoleMenuMapper;
import com.yegs.admin.business.service.IBaseRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yegs
 * @since 2020-08-20
 */
@Service
public class BaseRoleServiceImpl extends ServiceImpl<BaseRoleMapper, BaseRole> implements IBaseRoleService {


    @Autowired
    private BaseRoleMapper baseRoleMapper;

    @Autowired
    private BaseMenuMapper baseMenuMapper;

    @Autowired
    private BaseRoleMenuMapper baseRoleMenuMapper;


    public List<BaseRole> findRoleHaveMenu() {
        return baseRoleMapper.findRoleHaveMenu();
    }

    public List<BaseRole> findRoleHaveMenuList() {
        return baseRoleMapper.findRoleHaveMenuList();
    }


    public void saveRoleHaveMenuids(BaseRole baseRole){
        List<BaseMenu> menus = baseRole.getMenus();
        if(baseRole.getRoleId()==null){
            baseRoleMapper.insert(baseRole);
        }
        BaseRoleMenu baseRoleMenu = new BaseRoleMenu();
        baseRoleMenu.setRoleId(baseRole.getRoleId());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(baseRoleMenu);
        baseRoleMenuMapper.delete(queryWrapper);
        for(BaseMenu menu:menus){
            BaseRoleMenu roleMenu = new BaseRoleMenu();
            roleMenu.setRoleId(baseRole.getRoleId());
            roleMenu.setMenuId(menu.getMenuId());
            baseRoleMenuMapper.insert(roleMenu);
        }
    }
    @Override
    public List<String> findRoleHaveMenuListByRoleId(Integer roleId) {
        return baseRoleMapper.findRoleHaveMenuListByRoleId(roleId);
    }

    @Override
    public List<String> findRoleNameByUserName(String user_username) {
        return baseRoleMapper.findRoleNameByUserName(user_username);
    }

}
